// index.js
Page({
  data: {
    // 轮播图数据
    banners: [
      {
        id: 1,
        imageUrl: '/assets/images/banner1.jpg',
        link: '/pages/house/detail/detail?id=1'
      },
      {
        id: 2,
        imageUrl: '/assets/images/banner2.jpg',
        link: '/pages/activity/detail?id=1'
      },
      {
        id: 3,
        imageUrl: '/assets/images/banner3.jpg',
        link: '/pages/park/detail?id=1'
      }
    ],
    // 主要功能入口
    mainFunctions: [
      {
        id: 1,
        name: '找房子',
        icon: '/assets/icons/house-active.png',
        url: '/pages/house/house'
      },
      {
        id: 2,
        name: '公园服务',
        icon: '/assets/icons/park-active.png',
        url: '/pages/park/park'
      },
      {
        id: 3,
        name: '停车服务',
        icon: '/assets/icons/parking-active.png',
        url: '/pages/parking/parking'
      },
      {
        id: 4,
        name: '在线报修',
        icon: '/assets/icons/repair-active.png',
        url: '/pages/house/house?tab=repair'
      }
    ],
    // 房源推荐
    recommendHouses: [
      {
        id: 1,
        title: '阳光小区 2室1厅',
        imageUrl: '/assets/images/house1.jpg',
        tags: ['精装修', '南北通透'],
        price: 2800
      },
      {
        id: 2,
        imageUrl: '/assets/images/house2.jpg',
        title: '江景豪苑 3室2厅',
        area: 120,
        price: 3500,
        tags: ['南北通透', '配套齐全']
      }
    ],
    // 公园活动
    parkActivities: [
      {
        id: 1,
        title: '周末亲子活动',
        imageUrl: '/assets/images/activity1.jpg',
        date: '2024-03-30',
        location: '中心公园'
      },
      {
        id: 2,
        imageUrl: '/assets/images/activity2.jpg',
        title: '露天电影放映',
        date: '2024-03-17',
        location: '文化广场'
      }
    ]
  },

  onLoad() {
    this.getInitialData()
  },

  // 获取初始数据
  getInitialData() {
    // TODO: 调用API获取数据
  },

  // 搜索
  onSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },

  // 点击轮播图
  onBannerClick(e) {
    const { id } = e.currentTarget.dataset;
    const banner = this.data.banners.find(item => item.id === id);
    if (banner && banner.link) {
      wx.navigateTo({
        url: banner.link
      });
    }
  },

  // 点击功能入口
  onFunctionClick(e) {
    const { url } = e.currentTarget.dataset
    // 使用switchTab而不是navigateTo，因为这些都是tabBar页面
    wx.switchTab({ url })
  },

  // 点击房源
  onHouseClick(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/house/detail/detail?id=${id}`
    })
  },

  // 点击活动
  onActivityClick(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/park/activity/detail?id=${id}`
    })
  }
})
